package roleView 
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.loadingtypes.ImageItem;
	import br.com.stimuli.loading.loadingtypes.URLItem;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.LoaderContext;
	import flash.system.System;
	import flash.utils.ByteArray;
	import tools.ParseByteArray;
	/**
	 * 将加载的swf中的动作类保存起来，索引为("h"+roleID+"_"+act);
	 * getSkinClass(h1103_bigAttack) 返回 [class bigAttack]
	 * @author Kylin206
	 */
	public class LoaderSkinLib extends EventDispatcher implements ISkinLib 
	{
		public static const COMPLETED:String = "completed";
		public static const PROGRESS:String = "progress";
		private static var lib:Object = { };
		private static var _instance:LoaderSkinLib;
		
		public var loader:BulkLoader;
		public function LoaderSkinLib() 
		{
			loader = BulkLoader.getLoader("skinLoader");
			if (!loader)
				loader = new BulkLoader("skinLoader"); 
			loader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded); 
			loader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress); 
			_instance = this;
		}
		
		public static function getInstance():LoaderSkinLib
		{
			if (!_instance)_instance = new LoaderSkinLib();
			return _instance;
		}
		public static function clear():void
		{
			_instance = null;
		}
		/**
		 * 
		 * @param	url
		 * @param	roleID 如 "h1103"
		 */
		public function addSkin(url:String, roleID:String):void
		{
			loader.add(url, { id:roleID } );
		}
		
		public function startLoad()
		{
			loader.start(); 
		}
		
		
		private function onAllItemsLoaded(e:Event):void 
		{
			var items:Array = loader.items;
			var item:ImageItem;
			var $loader:Loader;
			var l:uint = items.length;
			var lenNames:uint = ActClassNames.names.length;
			var actName:String;
			var target:*;
			var urlItem:URLItem;
			var decode:Decode = new Decode();
			for (var i:uint = 0; i < l; i++)
			{
				if (items[i] is ImageItem)
				{
					item = items[i] as ImageItem;
					for (var j:uint = 0; j < lenNames; j++)
					{
						actName = item.id + "." + ActClassNames.names[j];
						storeSkins(actName, item.getDefinitionByName(actName) as Class);					
					}
				}
				else if (items[i] is URLItem)
				{
					urlItem = items[i];
					$loader = new Loader();
					var loaderContext:LoaderContext = new LoaderContext();
					loaderContext.allowCodeImport = true;
					$loader.loadBytes(decode.decode(urlItem.content as ByteArray, new Vector.<ByteArray>), loaderContext);
					
					$loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onloadComplete);
					$loader.name = urlItem.id + ".";					
				}				
			}
			
			loader.clear();
			
			dispatchEvent(new Event(COMPLETED));
			System.gc();
		}
		private function onloadComplete(evt:Event):void
		{
			var $loader:Loader = (evt.target as LoaderInfo).loader;
			$loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onloadComplete);
			var nam:String = $loader.name;
			var lenNames:uint = ActClassNames.names.length;
			var actName:String;
			for (var j:int = 0; j < lenNames; j++)
			{
				actName = nam + ActClassNames.names[j];
				try
				{
					storeSkins(actName, $loader.contentLoaderInfo.applicationDomain.getDefinition(actName) as Class);					
				}catch (err:Error) 
				{
					//trace("未找到动作:" + actName) 
				};
			}
		}
		
		private function onAllItemsProgress(e:Event):void 
		{
			//trace(loader.itemsTotal)
			//trace(e);
			/*if (loader._bytesTotal > 0)
			{
				UIFacade.showLoading("剩余资源加载" + Math.floor((loader._bytesLoaded / loader._bytesTotal) * 100) + "%");
			}*/
			dispatchEvent(new Event(PROGRESS));
		}
		
		private function storeSkins(id:String, mcClass:Class):void
		{
			if(mcClass)
			{
				lib[id] = mcClass;
			}
			else
			{
				trace("没有相应的类:", id)
			}
		}
		
		
		/* INTERFACE roleView.ISkinLib */
		
		public function getSkinClass(roleID:uint, actName:String):Class 
		{
			var c:Class = lib["h" + roleID + "." + actName] as Class;
			if (null == c)
			{
				throw new Error("找不到指定的动作类:" + "h" + roleID + "." + actName);
			}
			
			return c;
		}
		
	}

}
